package wjh.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import wjh.listenner.RecordsListenner;
import wjh.pojo.*;
import wjh.service.AdminService;
import wjh.service.DoctorService;
import wjh.service.PlanService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 功能
 * 作者：lyh
 * 日期：2024/9/3 17:15
 */
@Controller
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    //查找医生及科室信息
    @RequestMapping("/findDoctorsKe")
    @ResponseBody
    public List<Doctor> findDoctorsKe(){
        List<Doctor> doctorList = doctorService.findDoctorsKe();
//        model.addAttribute("doctorList", JSONArray.toJSONString(doctorList));
//        return "paiBan";
        return doctorList;
    }

    //查找医生及科室信息
    @RequestMapping("/findDoctorsKe2")
    public String findDoctorsKe2(Model model){
        List<Doctor> doctorList = doctorService.findDoctorsKe();
//        model.addAttribute("doctorList", JSONArray.toJSONString(doctorList));
        model.addAttribute("doctorList",doctorList);
        return "specialist2";
    }

    @RequestMapping("/findDoctors2")
    @ResponseBody
    public List<Doctor> findDoctors2(){
        List<Doctor> doctorList2 = doctorService.findDoctors2();
//        model.addAttribute("doctorList", JSONArray.toJSONString(doctorList));
//        return "paiBan";
        return doctorList2;
    }

    @Autowired
    private PlanService planService;
    @RequestMapping("/toUpdatePai")
    //接收参数 调用方法 做出响应
    public String toUpdateUser(Integer pageNum,Integer pageSize,Integer id,Model model) {

        Plan plan = planService.findPaiById(id);
        model.addAttribute("plan",plan);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        //跳转到修改界面
        return "doctor_updatePaiBan";
    }

    @RequestMapping("/updatePaiBan")
    //接收参数 调用方法 做出响应
    public String updatePaiBan(Integer pageNum, Integer pageSize, Integer id, Model model,
                               String doctor, Integer num1, String week, RedirectAttributes attr) {
        String[] s1 = doctor.split("-");
        int doctor_id = Integer.parseInt(s1[0]);
        Plan plan1 = planService.findByIdAndWeek(doctor_id, week);
        //排班校验 同一个医生一天只能有一条排班记录
        if (plan1 != null) {
            model.addAttribute("msg", "该医生该日已排班");
            return "doctor_updatePaiBan";
        } else {
            Plan plan = new Plan(id,doctor_id,num1,week);
//            System.out.println(plan.getId()+"-"+plan.getDoctor_id()+"-"+plan.getNumber()+"-"+plan.getWeek());
            doctorService.updatePaiBan(plan);
            attr.addAttribute("pageNum",pageNum);
            attr.addAttribute("pageSize",pageSize);
            return "redirect:/plan/findAllPai2.do";
        }
    }

    //查找所有病历信息
    @RequestMapping("/findAllRecord")
    public String findAllRecord(
            //当前页
            @RequestParam(required = false,defaultValue = "1")Integer pageNum,
            //默认展示三条
            @RequestParam(required = false,defaultValue = "3")Integer pageSize,
            Model model){
        //处理分页
        PageHelper.startPage(pageNum,pageSize);
        List<Records> recordsList= doctorService.findAllRecord();
        //得到pageInfo对象
        PageInfo<Records> pageInfo = new PageInfo<>(recordsList);
        model.addAttribute("page",pageInfo);
        return "recordsList";
    }

    //查找所有病历信息
    @RequestMapping("/findAllRecords")
    public String findAllRecords(
            //当前页
            @RequestParam(required = false,defaultValue = "1")Integer pageNum,
            //默认展示三条
            @RequestParam(required = false,defaultValue = "3")Integer pageSize,
            Model model,String likeName){
        //处理分页
        PageHelper.startPage(pageNum,pageSize);
        List<Records> recordsList= doctorService.findAllRecordsLike(likeName);
        //得到pageInfo对象
        PageInfo<Records> pageInfo = new PageInfo<>(recordsList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("likeName",likeName);
        return "recordsList";
    }

    //导出病历 不需要返回值 需要response对象设置响应头 响应类型
    @RequestMapping("/exportRecords")
    public void exportRecords(Integer pageNum, Integer pageSize, HttpServletResponse response,String likeName){
        doctorService.exportRecords(pageNum,pageSize,response,likeName);
    }

    //导入
    @RequestMapping("/importRecords")
    public String importRecords(MultipartFile file, HttpSession session){
        //session 保存重复数据 每次导入把session域清空掉
        doctorService.importRecords(file,session);
        //保存最新的
        session.setAttribute("recordList2", JSONArray.toJSONString(RecordsListenner.getList()));
        //增加完数据 重定向到展示界面
        return "redirect:/doctor/findAllRecords.do";
    }
}
